package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ChangeLog struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64      `json:"id" bson:"id,omitempty"`
	ItemId      int64      `json:"itemId" bson:"itemId"` //物品ID
	IoType      int64      `json:"ioType" bson:"ioType"` //出入库类型
	State       int64      `json:"state" bson:"state"`   //处理状态
	CreateAt    time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *ChangeLog) DocName() string { return "ChangeLog" }
func (this *ChangeLog) GetId() int64    { return this.Id }
func (this *ChangeLog) SetId(id int64)  { this.Id = id }
func (this *ChangeLog) Create() error {
	return this.GetFactory().Create(this)
}
func (this *ChangeLog) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *ChangeLog) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ChangeLog) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *ChangeLog) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ChangeLog) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *ChangeLog) ToString() string {
	return string(this.ToBytes())
}
func (this *ChangeLog) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ChangeLog) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ChangeLog) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *ChangeLog) ClearRelationsBeforeSave() mgo.Doc {
	return this
}
func neverUsed_ChangeLog() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ChangeLogAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *ChangeLog
	Session *ez.Session
}

func NewChangeLogAccessControl(model *ChangeLog, action string, session *ez.Session) *ChangeLogAccessControl {
	ctrl := &ChangeLogAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ChangeLogAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
